package com.huixh.train.member.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huixh.train.common.req.MemberTicketReq;
import com.huixh.train.common.resp.PageResult;
import com.huixh.train.member.domain.Ticket;
import com.huixh.train.member.domain.TicketExample;
import com.huixh.train.member.mapper.TicketMapper;
import com.huixh.train.member.req.TicketQueryReq;
import com.huixh.train.member.resp.TicketQueryResp;
import com.huixh.train.member.service.TicketService;
import com.huixh.train.utils.random.SnowUtil;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author huixh
*/
@Service
public class TicketServiceImpl implements TicketService {

    private final Logger logger = LoggerFactory.getLogger(TicketServiceImpl.class);
    @Resource
    private TicketMapper ticketMapper;

    @Resource
    private SnowUtil snowUtil;

    @Override
    public void save(MemberTicketReq ticketSaveReq) {
        // if (RootContext.getXID()!= null) {
        //     throw new RuntimeException("测试回滚");
        // }
        DateTime now = DateTime.now();
        Ticket ticket = BeanUtil.copyProperties(ticketSaveReq, Ticket.class);
        ticket.setId(snowUtil.getSnowflakeToLong());
        ticket.setCreateTime(now);
        ticket.setUpdateTime(now);
        ticketMapper.insert(ticket);
    }

    @Override
    @SentinelResource(value = "Tick.DoQueryList", blockHandler="queryListHandler")
    public PageResult <List<TicketQueryResp>> queryList(TicketQueryReq ticketQueryReq) {
        TicketExample ticketExample = new TicketExample();
        ticketExample.setOrderByClause("id desc");
        TicketExample.Criteria criteria = ticketExample.createCriteria();
        if (ObjUtil.isNotEmpty(ticketQueryReq.getMemberId())){
            criteria.andMemberIdEqualTo(ticketQueryReq.getMemberId());
        }

        logger.info("查询页码：{}", ticketQueryReq.getPageNum());
        logger.info("每页条数：{}", ticketQueryReq.getPageSize());
        PageHelper.startPage(ticketQueryReq.getPageNum(), ticketQueryReq.getPageSize());
        List<Ticket> ticketList = ticketMapper.selectByExample(ticketExample);

        PageInfo<Ticket> pageInfo = new PageInfo<>(ticketList);
        logger.info("总行数：{}", pageInfo.getTotal());
        logger.info("总页数：{}", pageInfo.getPages());

        List<TicketQueryResp> list = BeanUtil.copyToList(ticketList, TicketQueryResp.class);
        return PageResult.success(pageInfo.getTotal(), list);
    }

    public PageResult <List<TicketQueryResp>> queryListHandler(TicketQueryReq ticketQueryReq, BlockException throwable) {
        logger.error("接口限流");
        PageResult<List<TicketQueryResp>> result = new PageResult();
        result.setMessage("接口限流");
        return result;
    }

    @Override
    public void delete(Long id) {
        ticketMapper.deleteByPrimaryKey(id);
    }
}